<?php 
if(!defined('IN_CAI')) {
	exit('Access Denied');
}
/**
 * 兑奖管理
 * 
 */
$ac = $_REQUEST['ac'];
$url = '/admin/index.php?do=award';
if (empty($ac)) {
	$ac = 'list';
}
if ($ac == 'list') {
	$page = intval($_GET['page']) >0 ? intval($_GET['page']) : 1;
	$limit = 20;
	$offset = ($page - 1) *$limit;	
	$where = 'where username != "" and ifuserdel = 0 ';
	$ifsend = intval($_GET['ifsend']);
	if (isset($_GET['ifsend']) && $ifsend >-1) {
		$where .= " and ifsend = $ifsend";
	}
	$keyword = trim($_GET['keyword']);
	if (isset($_GET['keyword'])) {
		$where .= " and (username like '%{$keyword}%' || phone like '%{$keyword}%' || identity like '%{$keyword}%')";
	}
	$count = $db->result($db->query("select count(*) from ".tname('guess')." $where"),0);
	$query = $db->query("select * from ".tname('guess')." $where order by ifanswer desc,id desc limit $offset,$limit");
	$list = $db->fetch_all($query);
	$pagestr = multi($count,$limit,$page,$url);
	${'ifsend_'.$ifsend} = 'selected="selected"';
	include atemplate('do_award');
} elseif ($ac == 'del') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		updatetable('guess',array('ifuserdel'=>1),array('id'=>$id));
		if ($db->affected_rows()) {
			adminmessage('删除兑奖人成功',$url);
		}else {
			adminmessage('删除兑奖人失败',$url);
		}
	}
} elseif ($ac == 'send') {
	//发奖
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		updatetable('guess',array('ifsend'=>1),array('id'=>$id));
		if ($db->affected_rows()) {
			adminmessage('操作成功',$url);
		}else {
			adminmessage('操作失败',$url);
		}
	}
}
?>